{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45f76ccf",
   "metadata": {},
   "source": [
    "# 通用物体识别API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5f0061f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from Api_Key import*\n",
    "import base64\n",
    "import urllib\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "87327d36",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY=API_KET_BAIDU\n",
    "SECRET_KEY=SECRET_KEY_BAIDU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6cf63969",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_file_content_as_base64(path, urlencoded=False):\n",
    "    \"\"\"\n",
    "    获取文件base64编码\n",
    "    :param path: 文件路径\n",
    "    :param urlencoded: 是否对结果进行urlencoded \n",
    "    :return: base64编码信息\n",
    "    \"\"\"\n",
    "    with open(path, \"rb\") as f:\n",
    "        content = base64.b64encode(f.read()).decode(\"utf8\")\n",
    "        if urlencoded:\n",
    "            content = urllib.parse.quote_plus(content)\n",
    "    return content\n",
    "image_url=r'C:\\Users\\yly\\Pictures\\蒙古马.jpg'\n",
    "base_image=get_file_content_as_base64(image_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3a00223a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"result_num\":5,\"result\":[{\"score\":0.9994773,\"name\":\"非果蔬食材\"},{\"score\":0.00006788771,\"name\":\"菱角\"},{\"score\":0.000029983055,\"name\":\"豌豆芽\"},{\"score\":0.000027547449,\"name\":\"朝天椒\"},{\"score\":0.000027200325,\"name\":\"柑\"}],\"log_id\":1833516285860545264}\n"
     ]
    }
   ],
   "source": [
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 AK，SK 生成鉴权签名（Access Token）\n",
    "    :return: access_token，或是None(如果错误)\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f87605f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient?access_token=\" + get_access_token()\n",
    "    \n",
    "    # image 可以通过 get_file_content_as_base64(\"C:\\fakepath\\微信截图_20240910210450.png\",True) 方法获取\n",
    "    headers = {\n",
    "        'Content-Type': 'application/x-www-form-urlencoded',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    payload = {\"image\":base_image}\n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88744b2d",
   "metadata": {},
   "source": [
    "# 人脸识别API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3382b885",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient?access_token=\" + get_access_token()\n",
    "    \n",
    "    # image 可以通过 get_file_content_as_base64(\"C:\\fakepath\\微信截图_20240910210450.png\",True) 方法获取\n",
    "    headers = {\n",
    "        'Content-Type': 'application/x-www-form-urlencoded',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    payload = {\"image\":base_image}\n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eae8b3d5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "026ca11f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
